package com.warm.demo.mf.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.warm.demo.mf.api.dto.TestCommonDto;
import com.warm.demo.mf.api.vo.TestCommonVo;
import com.warm.demo.mf.domain.TestCommon;
import com.warm.demo.mf.mapper.TestCommonMapper;
import com.warm.demo.mf.service.ITestCommonService;
import com.warm.mf.core.domain.QueryParam;
import com.warm.mf.core.page.TableDataInfo;
import com.warm.mf.core.service.impl.BaseServiceImpl;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import org.noear.solon.annotation.Component;

import java.util.*;

import static com.warm.demo.mf.convert.TestCommonC.testCommonC;
import static com.warm.demo.mf.domain.table.TestCommonTableDef.test_common;
import static com.warm.mf.core.domain.QueryParam.getPage;

/**
 * 常规演示Service业务层处理
 *
 * @author warm
 * @date 2023-09-04
 */
@Component
public class TestCommonServiceImpl extends BaseServiceImpl<TestCommonMapper, TestCommon> implements ITestCommonService {


    @Override
    public TestCommonVo selectById(Long id) {
        TestCommon entity = getMapper().selectOneById(id);
        return testCommonC.convert(entity);
    }

    @Override
    public TableDataInfo<TestCommonVo> selectPage(QueryParam queryParam, TestCommonDto dto) {
        Page<TestCommonVo> page = buildQuery(dto).pageAs(getPage(queryParam), TestCommonVo.class);
        if (CollUtil.isEmpty(page.getRecords())) {
            page.setRecords(Collections.emptyList());
        }
        return TableDataInfo.build(page);
    }

    @Override
    public List<TestCommonVo> selectList(TestCommonDto dto) {
        return buildQuery(dto).listAs(TestCommonVo.class);
    }

    public QueryChain<TestCommon> buildQuery(TestCommonDto dto) {
        return this.queryChain()
                .where(test_common.title.eq(dto.getTitle()))
                .and(test_common.level.eq(dto.getLevel()))
                .and(test_common.send_doc_no.eq(dto.getSendDocNo()))
                .and(test_common.send_doc_unit.eq(dto.getSendDocUnit()))
                .and(test_common.publish_time.eq(dto.getPublishTime()))
                .and(test_common.deadline.eq(dto.getDeadline()))
                .and(test_common.label.eq(dto.getLabel()))
                .and(test_common.content.eq(dto.getContent()))
                .and(test_common.money.eq(dto.getMoney()))
                .and(test_common.views.eq(dto.getViews()))
                .and(test_common.new_file_id.eq(dto.getNewFileId()))
                .and(test_common.image_id.eq(dto.getImageId()));
    }

    @Override
    public int insertByDto(TestCommonDto dto) {
        TestCommon entity = testCommonC.convert(dto);
        return getMapper().insertSelective(entity);
    }

    @Override
    public int updateByDto(TestCommonDto dto) {
        TestCommon entity = testCommonC.convert(dto);
        return getMapper().update(entity);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return getMapper().deleteBatchByIds(Arrays.asList(ids));
    }
}
